#include "Data.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct s_Data 
{
	char *dato;
	int size;
};

Data data_fromString (char *s)
{
	Data d = NULL;
	d = (Data)calloc(1,sizeof(struct s_Data));
	d -> dato = NULL;
	if(d == NULL)
		return NULL;
	
	d -> dato = (char*)calloc(strlen(s)+1,sizeof(char));
	
	if(d -> dato == NULL)
	{
		free(d);
		d = NULL;
		return NULL;
	}
	
	strncpy(d -> dato,s,strlen(s)+1);
	d -> size = strlen(s);
	return d;
}

char* data_toString (Data d)
{
	char *retorno = NULL;
	if(d == NULL)
		return NULL;
	
	retorno = (char*)calloc(d -> size+1,sizeof(char));
	
	if(retorno == NULL)
		return NULL;
	
	strncpy(retorno,d -> dato,d -> size+1);
	return retorno;
}

int data_length (Data d)
{
	if(d == NULL)
		return -1;
	return d -> size;
}

Data data_clone (Data d)
{
	
	if (d == NULL)
		return  NULL;
	return data_fromString(d -> dato);
}

Data data_destroy(Data d)
{
	if(d == NULL)
		return NULL;
	free(d -> dato);
	free(d);
	d = NULL;
	return NULL;
}
